package com.example.demo.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.demo.domain.Supplier;
import com.example.demo.service.ISupplierService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2022-10-30
 */
@Controller
@RequestMapping("/supplier")
public class SupplierController {

    @Autowired
    ISupplierService service;

    @ApiOperation("Get supplier by id")
    @GetMapping("/{id}")
    public ResponseEntity<Supplier> getSupplier(@ApiParam("Supplier ID") @PathVariable long id){
        Supplier result = service.getById(id);
        if(result == null)
            return ResponseEntity.noContent().build();
        else
            return ResponseEntity.ok(result);
    }

    @ApiOperation("Find supplier")
    @GetMapping("")
    public ResponseEntity<IPage<Supplier>> findSupplier(@ApiParam("Supplier name")String name){
        IPage<Supplier> result = service.findSuppliers(name,3,3);
        return ResponseEntity.ok(result);
    }

    @ApiOperation("Add supplier")
    @PostMapping("")
    public ResponseEntity<Supplier> addSupplier(@RequestBody Supplier supplier){
        Supplier result = service.addSupplier(supplier);
        return ResponseEntity.ok(result);
    }

    @ApiOperation("Update Supplier info")
    @PutMapping("/{id}")
    public ResponseEntity<Void> updateSupplier(@PathVariable long id,@RequestBody Supplier supplier){
        service.updateById(supplier);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteSupplier(@PathVariable long id){
        service.removeById(id);
        return ResponseEntity.ok().build();
    }

}

